package com.example.courseplus.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import com.example.courseplus.Adapter.ViewPagerAdapter;
import com.example.courseplus.DAO.TeacherDAO;
import com.example.courseplus.Entity.Teacher;
import com.example.courseplus.Fragment.TeacherCourse;
import com.example.courseplus.Fragment.TeacherInfo;
import com.example.courseplus.Fragment.TeacherPublish;
import com.example.courseplus.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class TeacherActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private MenuItem menuItem;
    private BottomNavigationView bottomNavigationView;
    Toolbar toolbar;
    TeacherDAO teacherDAO = new TeacherDAO();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher);
        viewPager = findViewById(R.id.teacher_pager);
        bottomNavigationView = findViewById(R.id.teacher_bnv);
        toolbar = findViewById(R.id.teacher_bar);
        toolbar.setTitle("你好，"+getUserName()+"老师");
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.tea_course:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.tea_publish:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.tea_info:
                        viewPager.setCurrentItem(2);
                        break;
                    default:
                        break;
                }
                return false;
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if(menuItem!=null){
                    menuItem.setChecked(false);
                }else {
                    bottomNavigationView.getMenu().getItem(0).setChecked(false);
                }
                menuItem = bottomNavigationView.getMenu().getItem(position);
                menuItem.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        setupViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),0);
        TeacherCourse teacherCourse = new TeacherCourse();
        TeacherPublish teacherPublish = new TeacherPublish();
        TeacherInfo teacherInfo = new TeacherInfo();
        viewPagerAdapter.addFragment(teacherCourse);
        viewPagerAdapter.addFragment(teacherPublish);
        viewPagerAdapter.addFragment(teacherInfo);
        viewPager.setAdapter(viewPagerAdapter);
    }

    public String getUserName(){
        return getIntent().getStringExtra("username");

    }

    public Teacher getTeacher(){
        String userName = getUserName();
        return teacherDAO.selectTeacherByUserName(userName);
    }
}